package com.liucxu.qiushimeinv.View.Activity;

import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.liucxu.qiushimeinv.Base.BaseActivity;
import com.liucxu.qiushimeinv.Consts.Consts;
import com.liucxu.qiushimeinv.Model.Bean.BaseBean;
import com.liucxu.qiushimeinv.Model.Bean.LoginResponseBean;
import com.liucxu.qiushimeinv.Presenter.ILoginPresenter;
import com.liucxu.qiushimeinv.Presenter.Impl.LoginPresenterImpl;
import com.liucxu.qiushimeinv.R;
import com.liucxu.qiushimeinv.Utils.PreferenceUtils;
import com.liucxu.qiushimeinv.View.IView.ILoginView;

/**
 * 欢迎页面实现自动登陆
 * Created by liukanshan on 2017/1/1.
 */

public class SplashActivity extends BaseActivity implements ILoginView {

    ImageView splash_imv;
    private ScaleAnimation scaleAnimation;
    private ILoginPresenter presenter;

    @Override
    public void initBeforeView() {
        //设置无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    public int getInflateLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void setupView() {
        splash_imv = $(R.id.splash_imv);
        scaleAnimation = new ScaleAnimation(1f, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(2000);
        scaleAnimation.setFillAfter(true);
        splash_imv.setAnimation(scaleAnimation);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                SplashActivity.this.startActivity(intent);
                SplashActivity.this.finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        presenter = new LoginPresenterImpl(this);
    }

    @Override
    public void setupData() {
        if ((!TextUtils.isEmpty(PreferenceUtils.GetUserName())) && (!TextUtils.isEmpty(PreferenceUtils.GetUserPassword()))) {
            presenter.login(PreferenceUtils.GetUserName(), PreferenceUtils.GetUserPassword());
        } else {
            scaleAnimation.start();
        }
    }

    @Override
    public void setUserView(LoginResponseBean loginInfo) {
        PreferenceUtils.SaveUserToken(loginInfo.getToken());
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void showError(BaseBean bean) {
        if (bean.getCode() == Consts.NET_ERROR) {
            Snackbar.make(splash_imv, bean.getDesc(), Snackbar.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
